package local;

import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.converters.ArffSaver;

public class CreateWeka {

	private static String workName; 
	private static Vector<String> featureVec;
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		if(CreateVector.workingMode == CreateVector.TWENTY_FILES){
			workName = "20files";
		}else if(CreateVector.workingMode == CreateVector.TWENTY_FILES_WITH_INVERSE){
			workName = "20filesWithInverse";
		}else if(CreateVector.workingMode == CreateVector.ALL_FILES){
			workName = "allFiles";
		}else if(CreateVector.workingMode == CreateVector.ALL_FILES_WITH_INVERSE){
			workName = "allFilesWithInverse";
		} else  if (CreateVector.workingMode == CreateVector.LOCAL) {
			workName = "0";
		}
		createVector();
	
		System.out.println(featureVec.size());
		
		FastVector fvWekaVector = new FastVector(featureVec.size() + 1);
		for (String string : featureVec) {
			fvWekaVector.addElement(new Attribute(string));
		}
		
		FastVector fvClassVal = new FastVector(2);
		fvClassVal.addElement("false");
		fvClassVal.addElement("true");
		Attribute boolAttr = new Attribute("boolAttr", fvClassVal);
		fvWekaVector.addElement(boolAttr);

		
		Instances isTrainingSet = new Instances("Rel", fvWekaVector, 0);
		isTrainingSet.setClassIndex(featureVec.size());

		createTrainingSet(isTrainingSet, fvWekaVector);
		
		Toolkit.getDefaultToolkit().beep();

	}
	
	private static void createTrainingSet(Instances isTrainingSet, FastVector fvWekaVector ) {
		ArffSaver saver = new ArffSaver();
		URL filePate = null;
		
		try {
			saver.setFile(new File(workName + ".arff"));
			saver.setStructure(isTrainingSet);
			saver.writeBatch();
			
			File file = new File(workName + ".arff");
			FileOutputStream out = new FileOutputStream(file, true);
			ArrayList<Integer> positions = null;
			FileReader reader;
			for(int i = 0 ; i <= 16 ; i++)
			{
				String fileNum = i<10 ? "0"+i : ""+i ;
				BufferedReader in = null;
				if (CreateVector.workingMode == CreateVector.LOCAL) {
					reader = new FileReader(workName + "/known-r-000" + fileNum);
					in = new BufferedReader(reader);
				} else {
					filePate = new URL("https://s3.amazonaws.com/ahgfinal/result/" + workName + "/Step2/known-r-000" + fileNum);
					in = new BufferedReader(new InputStreamReader(filePate.openStream()));
				}
				String inputLine;
				boolean ignoreLine = true;
				while ((inputLine = in.readLine()) != null)
				{
					if(inputLine.equals("True") || inputLine.equals("False"))
					{
						Collections.sort(positions);
						for (Integer integer : positions) {
							out.write(("" + integer + " 1,").getBytes());
						}
					
							out.write(("" + (fvWekaVector.size()-1) + " " + inputLine.toLowerCase() + "}").getBytes());
					}
					else if(ignoreLine)
					{
						ignoreLine = false;
						out.write("{".getBytes());
						positions = new ArrayList<Integer>();
					}
					else if(inputLine.isEmpty())
					{
						ignoreLine = true;
						out.write("\n".getBytes());
					}
					else
					{
						int pos = featureVec.indexOf(inputLine);
						positions.add(new Integer(pos));
					}
				}
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}

	private static void createVector() {

		featureVec = new Vector<String>();
		
		try {
			File file = new File(workName + "Vec.txt");
		    BufferedReader br = new BufferedReader(new FileReader(file));
		    String line;
		    while ((line = br.readLine()) != null) {
		       featureVec.add(line);
		    }
		    br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
